<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>交互式HTML学习工具</title>
    <style>
        :root {
            --primary: #3498db;
            --secondary: #2c3e50;
            --accent: #e74c3c;
            --light: #ecf0f1;
            --gray: #7f8c8d;
            --success: #2ecc71;
        }
        
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            line-height: 1.6;
            color: #333;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
            padding: 20px;
        }
        
        .container {
            max-width: 1400px;
            margin: 0 auto;
            display: grid;
            grid-template-columns: 250px 1fr 1fr;
            grid-template-rows: auto 1fr;
            gap: 20px;
            grid-template-areas:
                "header header header"
                "tags editor preview";
        }
        
        header {
            grid-area: header;
            background: var(--secondary);
            color: white;
            padding: 20px;
            border-radius: 10px;
            text-align: center;
            margin-bottom: 20px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        
        h1 {
            font-size: 2.2rem;
            margin-bottom: 10px;
        }
        
        .subtitle {
            color: var(--light);
            font-weight: 300;
        }
        
        .tags-container {
            grid-area: tags;
            background: white;
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            overflow-y: auto;
        }
        
        .tags-title {
            color: var(--secondary);
            margin-bottom: 15px;
            padding-bottom: 10px;
            border-bottom: 2px solid var(--light);
        }
        
        .tag-list {
            display: flex;
            flex-direction: column;
            gap: 10px;
        }
        
        .tag-item {
            background: var(--light);
            padding: 10px 15px;
            border-radius: 5px;
            cursor: grab;
            transition: all 0.3s ease;
            font-family: 'Courier New', monospace;
            font-weight: 500;
            border-left: 4px solid var(--primary);
        }
        
        .tag-item:hover {
            background: var(--primary);
            color: white;
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }
        
        .tag-item:active {
            cursor: grabbing;
        }
        
        .tag-desc {
            font-size: 0.8rem;
            color: var(--gray);
            margin-top: 5px;
        }
        
        .tag-item:hover .tag-desc {
            color: rgba(255, 255, 255, 0.8);
        }
        
        .editor-container {
            grid-area: editor;
            background: white;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            display: flex;
            flex-direction: column;
            overflow: hidden;
        }
        
        .editor-header {
            background: var(--primary);
            color: white;
            padding: 15px 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        #code-editor {
            flex: 1;
            width: 100%;
            min-height: 400px;
            padding: 20px;
            font-family: 'Courier New', monospace;
            font-size: 16px;
            line-height: 1.5;
            border: none;
            resize: none;
            background: #2c3e50;
            color: #ecf0f1;
            tab-size: 4;
        }
        
        .preview-container {
            grid-area: preview;
            background: white;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            display: flex;
            flex-direction: column;
            overflow: hidden;
        }
        
        .preview-header {
            background: var(--primary);
            color: white;
            padding: 15px 20px;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        
        .browser-ui {
            display: flex;
            align-items: center;
            background: #f1f2f6;
            padding: 8px 15px;
            border-bottom: 1px solid #ddd;
        }
        
        .browser-dots {
            display: flex;
            gap: 6px;
            margin-right: 10px;
        }
        
        .browser-dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        
        .browser-dot:nth-child(1) {
            background: #ff5f57;
        }
        
        .browser-dot:nth-child(2) {
            background: #febc2e;
        }
        
        .browser-dot:nth-child(3) {
            background: #28c840;
        }
        
        .browser-url {
            flex: 1;
            background: white;
            padding: 5px 10px;
            border-radius: 15px;
            font-size: 0.8rem;
            color: #666;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
        
        #preview {
            flex: 1;
            padding: 20px;
            overflow: auto;
            background: white;
        }
        
        .page-title {
            font-size: 1rem;
            font-weight: 500;
            margin: 0;
            color: white;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            max-width: 200px;
        }
        
        .btn {
            background: var(--accent);
            color: white;
            border: none;
            padding: 8px 15px;
            border-radius: 5px;
            cursor: pointer;
            transition: background 0.3s;
            font-weight: 500;
        }
        
        .btn:hover {
            background: #c0392b;
        }
        
        .btn-clear {
            background: var(--gray);
        }
        
        .btn-clear:hover {
            background: #636e72;
        }
        
        .dropzone {
            border: 2px dashed var(--primary);
            border-radius: 5px;
            padding: 10px;
            text-align: center;
            color: var(--gray);
            margin-top: 10px;
            transition: all 0.3s;
        }
        
        .dropzone.active {
            background: rgba(52, 152, 219, 0.1);
            color: var(--primary);
        }
        
        @media (max-width: 1024px) {
            .container {
                grid-template-columns: 1fr;
                grid-template-areas:
                    "header"
                    "tags"
                    "editor"
                    "preview";
            }
            
            .tags-container {
                max-height: 200px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>交互式HTML学习工具</h1>
            <p class="subtitle">拖放标签、编辑代码、实时预览 - 轻松学习HTML</p>
        </header>
        
        <div class="tags-container">
            <h2 class="tags-title">HTML标签库</h2>
            <p>拖动标签到代码编辑器或点击插入</p>
            
            <div class="tag-list">
                <div class="tag-item" data-tag="<h1>标题</h1>" data-container="body">
                    &lt;h1&gt; - 标题
                    <div class="tag-desc">一级标题</div>
                </div>
                <div class="tag-item" data-tag="<p>段落文本</p>" data-container="body">
                    &lt;p&gt; - 段落
                    <div class="tag-desc">文本段落</div>
                </div>
                <div class="tag-item" data-tag='<a href="#">链接</a>' data-container="body">
                    &lt;a&gt; - 链接
                    <div class="tag-desc">超链接</div>
                </div>
                <div class="tag-item" data-tag='<img src="https://www.pngmart.com/files/11/Doge-Meme-PNG-Free-Download.png" alt="图片" width="150">' data-container="body">
                    &lt;img&gt; - 图片
                    <div class="tag-desc">嵌入图像</div>
                </div>
                <div class="tag-item" data-tag="<ul><li>列表项</li></ul>" data-container="body">
                    &lt;ul&gt; - 无序列表
                    <div class="tag-desc">项目符号列表</div>
                </div>
                <div class="tag-item" data-tag="<ol><li>列表项</li></ol>" data-container="body">
                    &lt;ol&gt; - 有序列表
                    <div class="tag-desc">编号列表</div>
                </div>
                <div class="tag-item" data-tag="<div>容器</div>" data-container="body">
                    &lt;div&gt; - 分区
                    <div class="tag-desc">内容容器</div>
                </div>
                <div class="tag-item" data-tag="<span>内联文本</span>" data-container="body">
                    &lt;span&gt; - 范围
                    <div class="tag-desc">内联容器</div>
                </div>
                <div class="tag-item" data-tag="<button>按钮</button>" data-container="body">
                    &lt;button&gt; - 按钮
                    <div class="tag-desc">可点击按钮</div>
                </div>
                <div class="tag-item" data-tag="<input type='text' placeholder='输入框'>" data-container="body">
                    &lt;input&gt; - 输入框
                    <div class="tag-desc">用户输入</div>
                </div>
                <div class="tag-item" data-tag="<title>页面标题</title>" data-container="head">
                    &lt;title&gt; - 页面标题
                    <div class="tag-desc">浏览器标签标题</div>
                </div>
                <div class="tag-item" data-tag='<meta name="description" content="页面描述">' data-container="head">
                    &lt;meta&gt; - 元数据
                    <div class="tag-desc">页面元信息</div>
                </div>
                <div class="tag-item" data-tag='<link rel="stylesheet" href="style.css">' data-container="head">
                    &lt;link&gt; - 外部资源
                    <div class="tag-desc">链接外部CSS</div>
                </div>
            </div>
            
            <div class="dropzone" id="editor-dropzone">
                拖放标签到此处添加到编辑器
            </div>
        </div>
        
        <div class="editor-container">
            <div class="editor-header">
                <h2>代码编辑器</h2>
                <div>
                    <button class="btn btn-clear" id="clear-btn">重置</button>
                    <button class="btn" id="run-btn">运行</button>
                </div>
            </div>
            <textarea id="code-editor" placeholder="在此编写或拖放HTML代码..."></textarea>
        </div>
        
        <div class="preview-container">
            <div class="preview-header">
                <h2 class="page-title" id="page-title">页面预览</h2>
            </div>
            <div class="browser-ui">
                <div class="browser-dots">
                    <div class="browser-dot"></div>
                    <div class="browser-dot"></div>
                    <div class="browser-dot"></div>
                </div>
                <div class="browser-url" id="browser-url">https://example.com</div>
            </div>
            <div id="preview"></div>
        </div>
    </div>

    <script>
        // 初始HTML代码模板
        const initialHTML = `<!DOCTYPE html>
<html>
<head>
    <title>我的网页</title>
</head>
<body>
    <h1>欢迎使用HTML学习工具</h1>
    <p>从这里开始您的HTML之旅！</p>
</body>
</html>`;

        document.addEventListener('DOMContentLoaded', function() {
            const codeEditor = document.getElementById('code-editor');
            const preview = document.getElementById('preview');
            const pageTitle = document.getElementById('page-title');
            const browserUrl = document.getElementById('browser-url');
            const runBtn = document.getElementById('run-btn');
            const clearBtn = document.getElementById('clear-btn');
            const dropzone = document.getElementById('editor-dropzone');
            const tagItems = document.querySelectorAll('.tag-item');
            
            // 初始化编辑器和预览
            codeEditor.value = initialHTML;
            updatePreview();
            
            // 运行按钮点击事件
            runBtn.addEventListener('click', updatePreview);
            
            // 重置按钮点击事件
            clearBtn.addEventListener('click', function() {
                codeEditor.value = initialHTML;
                updatePreview();
            });
            
            // 设置标签拖放功能
            tagItems.forEach(tag => {
                tag.setAttribute('draggable', 'true');
                
                tag.addEventListener('dragstart', function(e) {
                    e.dataTransfer.setData('text/plain', tag.getAttribute('data-tag'));
                    dropzone.classList.add('active');
                });
                
                tag.addEventListener('dragend', function() {
                    dropzone.classList.remove('active');
                });
                
                tag.addEventListener('click', function() {
                    const tagContent = tag.getAttribute('data-tag');
                    const container = tag.getAttribute('data-container');
                    insertTagToContainer(tagContent, container);
                    updatePreview();
                });
            });
            
            // 设置放置区域
            dropzone.addEventListener('dragover', function(e) {
                e.preventDefault();
                dropzone.classList.add('active');
            });
            
            dropzone.addEventListener('dragleave', function() {
                dropzone.classList.remove('active');
            });
            
            dropzone.addEventListener('drop', function(e) {
                e.preventDefault();
                dropzone.classList.remove('active');
                const tag = e.dataTransfer.getData('text/plain');
                insertTagAtCursor(tag);
                updatePreview();
            });
            
            // 代码编辑器接受拖放
            codeEditor.addEventListener('dragover', function(e) {
                e.preventDefault();
            });
            
            codeEditor.addEventListener('drop', function(e) {
                e.preventDefault();
                const tag = e.dataTransfer.getData('text/plain');
                insertTagAtCursor(tag);
                updatePreview();
            });
            
            // 在光标位置插入标签
            function insertTagAtCursor(tag) {
                const cursorPos = codeEditor.selectionStart;
                const currentValue = codeEditor.value;
                const newValue = currentValue.substring(0, cursorPos) + 
                                tag + 
                                currentValue.substring(cursorPos);
                
                codeEditor.value = newValue;
                
                // 设置光标位置到插入的内容之后
                const newCursorPos = cursorPos + tag.length;
                codeEditor.setSelectionRange(newCursorPos, newCursorPos);
                codeEditor.focus();
            }
            
            // 插入标签到指定容器（head或body）
            function insertTagToContainer(tag, container) {
                const editorContent = codeEditor.value;
                let insertPosition;
                
                if (container === 'head') {
                    // 在</head>标签前插入
                    const headClosePos = editorContent.indexOf('</head>');
                    if (headClosePos !== -1) {
                        // 找到<head>内的最后一个标签位置
                        const headContent = editorContent.substring(
                            editorContent.indexOf('<head>') + 6, 
                            headClosePos
                        );
                        
                        // 如果head内有内容，找到最后一个标签的结束位置
                        const lastTagEnd = headContent.lastIndexOf('>');
                        if (lastTagEnd !== -1) {
                            insertPosition = editorContent.indexOf('<head>') + 6 + lastTagEnd + 1;
                        } else {
                            // 如果head内没有内容，直接在<head>后插入
                            insertPosition = editorContent.indexOf('<head>') + 6;
                        }
                        
                        // 格式化插入的代码
                        const indent = getIndentationAtPosition(editorContent, insertPosition);
                        const formattedTag = `\n${indent}    ${tag}`;
                        
                        codeEditor.value = 
                            editorContent.substring(0, insertPosition) + 
                            formattedTag + 
                            editorContent.substring(insertPosition);
                    }
                } else if (container === 'body') {
                    // 在</body>标签前插入
                    const bodyClosePos = editorContent.indexOf('</body>');
                    if (bodyClosePos !== -1) {
                        // 找到<body>内的最后一个标签位置
                        const bodyContent = editorContent.substring(
                            editorContent.indexOf('<body>') + 6, 
                            bodyClosePos
                        );
                        
                        // 如果body内有内容，找到最后一个标签的结束位置
                        const lastTagEnd = bodyContent.lastIndexOf('>');
                        if (lastTagEnd !== -1) {
                            insertPosition = editorContent.indexOf('<body>') + 6 + lastTagEnd + 1;
                        } else {
                            // 如果body内没有内容，直接在<body>后插入
                            insertPosition = editorContent.indexOf('<body>') + 6;
                        }
                        
                        // 格式化插入的代码
                        const indent = getIndentationAtPosition(editorContent, insertPosition);
                        const formattedTag = `\n${indent}    ${tag}`;
                        
                        codeEditor.value = 
                            editorContent.substring(0, insertPosition) + 
                            formattedTag + 
                            editorContent.substring(insertPosition);
                    }
                }
                
                codeEditor.focus();
            }
            
            // 获取指定位置的缩进
            function getIndentationAtPosition(content, position) {
                // 找到当前位置所在行的开始
                let lineStart = position;
                while (lineStart > 0 && content[lineStart - 1] !== '\n') {
                    lineStart--;
                }
                
                // 提取该行的缩进（空格和制表符）
                let indent = '';
                let i = lineStart;
                while (i < content.length && (content[i] === ' ' || content[i] === '\t')) {
                    indent += content[i];
                    i++;
                }
                
                return indent;
            }
            
            // 更新预览
            function updatePreview() {
                try {
                    preview.innerHTML = codeEditor.value;
                    
                    // 提取并显示页面标题
                    const titleMatch = codeEditor.value.match(/<title>(.*?)<\/title>/i);
                    if (titleMatch && titleMatch[1]) {
                        pageTitle.textContent = titleMatch[1];
                        document.title = titleMatch[1] + " - HTML学习工具";
                    } else {
                        pageTitle.textContent = "页面预览";
                        document.title = "交互式HTML学习工具";
                    }
                    
                    // 更新浏览器URL显示
                    const urlMatch = codeEditor.value.match(/<meta.*?content="(.*?)".*?>/i);
                    if (urlMatch && urlMatch[1]) {
                        browserUrl.textContent = urlMatch[1];
                    } else {
                        browserUrl.textContent = "https://example.com";
                    }
                    
                } catch (e) {
                    preview.innerHTML = `<div style="color: red; padding: 20px;">
                        <h3>错误</h3>
                        <p>${e.message}</p>
                    </div>`;
                }
            }
        });
    </script>
</body>
</html>